<?php
include_once('./db.conf');
session_start();
// Connects to your Database 
$connection=mysqli_connect(DBHOST, DBUSER, DBPASS,DBDBNAME); 
if(!$connection)die ('Something Went Wrong...');
if(isset($_COOKIE['CompanyCookie'])){
//if the login form is submitted

if (isset($_POST['submit'])) { // if form has been submitted


// makes sure they filled it in
	
	if(!$_POST['questName'] | !$_POST['question'] | !$_POST['startDay'] | !$_POST['startMonth'] | !$_POST['startYear'] | !$_POST['startHour'] | !$_POST['endDay'] | !$_POST['endMonth'] | !$_POST['endYear'] | !$_POST['endHour'] | !$_POST['amountOf']) {
		die('You did not fill in a required field.');
	}
	else{
		if(!$_POST['male'] && !$_POST['female'])
			die('Please Select A Gender');
		
		else{
			if($_POST['notAllowed']=="" && $_POST['allowed']=="")
				die('Please Define If You Want Other People To See The Survey');
			else if($_POST['notAllowed']=="")
				$allowed=1;
			else
				$allowed=0;
			//makes sure all the numeric charecters are integer
			$_POST['startYear']=(int)$_POST['startYear'];if($_POST['startYear']==0)die('Start Time "Year" Must Be A Number');
			$_POST['endYear']=(int)$_POST['endYear'];if($_POST['endYear']==0)die('End Time "Year" Must Be A Number');
			$_POST['amountOf']=(int)$_POST['amountOf'];if($_POST['amountOf']==0)die('Number Of People Has To Be A Number');
			else{
				//if the textbox is empty, it means that all age groups are involved for the survey
				if($_POST['age']=="")
					$_POST['age']=0;
				else //converts the string 'age' to integer
					$age=(int)$_POST['age'];//if the age is string returns 0
				//if the textbox is empty, it means that there are more than one answers
				if($_POST['answers']=="")
					$_POST['answers']=0;
				else //converts the string 'answers' to integer
					$_POST['answers']=(int)$_POST['answers'];
				$startTime=$_POST['startYear']."-".(int)$_POST['startMonth']."-".(int)$_POST['startDay'];
				$endTime=$_POST['endYear']."-".(int)$_POST['endMonth']."-".(int)$_POST['endDay'];
				//checking if start time is older than today's date
				if((strtotime($startTime)-strtotime(date('Y-m-d')))<0)
					die('Start Time Should Be Older Than Todays Date');
				//checking if end time is older than start time
				if((strtotime($endTime)-strtotime($startTime))<0)
					die('End Time Should Be Older Than Start Time');
				//checking start date if it s valid
				if($_POST['startMonth']=="02" && $_POST['startDay']>29)
					die('Check The Start Date Again...');
				if($_POST['startMonth']=="02" && $_POST['startYear']%4!=0 && $_POST['startDay']>28)
					die('Check The Start Date Again...');
				//checking end date if it s valid
				if($_POST['endMonth']=="02" && $_POST['endDay']>29)
					die('Check The End Date Again...');
				if($_POST['endMonth']=="02" && $_POST['endYear']%4!=0 && $_POST['endDay']>28)
					die('Check The End Date Again...');
				//if everything is ok add the hour to start and end times
				$startTime=$startTime." ".$_POST['startHour'];
				$endTime=$endTime." ".$_POST['endHour'];
				//checks the target groups age
				$year=(int)date('Y')-$age;
				if($age==0)
					$year=0;
				//arranging gender of the target group
				if($_POST['male']=="m" && $_POST['female']=="f")
					$gender=" ";
				else if($_POST['female']=="f")
					$gender="f";
				else $gender="m";
			}
		}
		// checks it against the database
		$query1 = "call createSurvey($_SESSION[comID],'$_POST[question]','$startTime','$endTime','$_POST[questName]',$_POST[answers],$allowed)";
		if(!$check1 = mysqli_query($connection,$query1))
			die(mysqli_error($connection));
		else{
			$query2="call getSurveyForCompany($_SESSION[comID])";
			if($check2=mysqli_query($connection,$query2)){
				while($row=mysqli_fetch_assoc($check2)){//gets all surveyID
					$surveyID=$row['surveyId'];
				}
			}
			//now we have the last added surveyId
			//selecting and adding participants to participants table
			//these two lines added because mysql doesnt allow to run the commands below and I dont know why. I had to close and reestablish the connection
			mysqli_close($connection);
			$connection=mysqli_connect(DBHOST, DBUSER, DBPASS,DBDBNAME);
			$query3="call selectCustomersForSurvey($surveyID,$year,'$gender',$_POST[amountOf])";
			if(!$check3= mysqli_query($connection,$query3))
				die(mysqli_error($connection));
			else
				header("Location: index.php");
		}
	}
}
 else {	

// if they are not logged in
?>

<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr><td colspan=2><h1>Define A Survey</h1></td></tr>
<tr><td>Question Name:</td><td>
<input type="text" name="questName" maxlength="30">
</td></tr>
<tr><td>Question:</td><td>
<input type="text" multiline="true" name="question" maxlength="160">
</td></tr>
<tr><td>Number of Answers:</td><td>
<input type="text" name="answers" maxlength="20">
</td></tr>
<tr><td>Start Time:</td><td>
Day: 
<select name="startDay" >
	<option value="">Select A Day
	<?php for($i=1;$i<=31;$i++){
		if($i<10)$value="0".$i;
		else $value=$i;
	?>
	<option value="<?php echo $value?>"><?php echo $value?>
<?php
}
?>
</select>
Month:
<select name="startMonth">
	<option value="">Select A Month
	<option value="01">January<option value="02">February<option value="03">March<option value="04">April<option value="05">May
	<option value="06">June<option value="07">July<option value="08">August<option value="09">September<option value="10">October
	<option value="11">November<option value="12">December
</select>
Year:
<input type="text" name="startYear">
Hour:
<select name="startHour" >
	<option value="">Select
	<?php for($i=0;$i<=23;$i++){
		if($i<10)$value="0".$i.":00";
		else $value=$i.":00";
	?>
	<option value="<?php echo $value?>"><?php echo $value?>
<?php
}
?>
</select>
</td></tr>
<tr><td>End Time:</td><td>
Day: 
<select name="endDay" >
	<option value="">Select A Day
	<?php for($i=1;$i<=31;$i++){
		if($i<10)$value="0".$i;
		else $value=$i;
	?>
	<option value="<?php echo $value?>"><?php echo $value?>
<?php
}
?>
</select>
Month:
<select name="endMonth">
	<option value="">Select A Month
	<option value="01">January<option value="02">February<option value="03">March<option value="04">April<option value="05">May
	<option value="06">June<option value="07">July<option value="08">August<option value="09">September<option value="10">October
	<option value="11">November<option value="12">December
</select>
Year:
<input type="text" name="endYear">
Hour:
<select name="endHour" >
	<option value="">Select
	<?php for($i=0;$i<=23;$i++){
		if($i<10)$value="0".$i.":00";
		else $value=$i.":00";
	?>
	<option value="<?php echo $value?>"><?php echo $value?>
<?php
}
?>
</select>
</td></tr>
<tr><td>Do You Want Other People To See The Result Of The Survey?</td><td>
<input  type="radio" name="allowed" value="1">YES
<input type="radio" name="notAllowed" value="0">NO
</td></tr>
<td></td>
<tr></tr>
<td></td>
<tr></tr>
<td></td>
<tr><td colspan=2><h1>Define A Target Group</h1></td></tr>
<tr><td>Number Of People:</td><td>
<input type="text" name="amountOf" >
</td></tr>
<tr><td>Age(Younger Than):</td><td>
<input type="text" name="age" maxlength="4">
(If For All Ages Leave It Blank)
</td></tr>
<tr><td>Male:</td><td>
<input type="checkbox" name="male" value="m" >
</td></tr>
<tr><td>Female:</td><td>
<input type="checkbox" name="female" value="f" >
</td></tr>
<tr><td colspan="2" align="right">
<input type="submit" name="submit" value="Submit">
</td></tr>
</table>
</form>
<?php

}
mysqli_close($connection);
}
else
	die('Session Expired Please Login...');
?>
